package com.test.Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Servlet implementation class SendEMailServlet
 */
@WebServlet("/com/test/servlet/SendServlet")
public class SendEMailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SendEMailServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 通过Properties属性将邮箱的地址、协议、端口号写入，然后放到session中
		Properties props = new Properties();
		// 邮箱的协议；有很多 POP3,SMTP,IMAP，在这里使用 SMTP协议
		props.setProperty("mail.transport.protocol", "smtp");
		// qq邮箱使用smtp协议的地址
		props.setProperty("mail.smtp.host", "smtp.163.com");
		// qq邮箱smtp的端口号有两个 465和587
		props.setProperty("mail.smtp.port", "25");
		// 邮箱是否需要授权
		props.setProperty("mail.smtp.auth", "true");
		// 若是163邮箱则已经结束了，若是qq邮箱需要SSL安全认证

		// java针对qq的安全认证是通过 javax.net.ssl.SSLSocketFactory全类名的抽象类来实现的
//		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		// 此邮箱只针对经过安全认证的邮箱有效
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		// 安全认证的端口号，一边和邮箱的端口号一致
		props.setProperty("mail.smtp.socketFactory.port", "25");
		Session session = Session.getInstance(props);
		// 开启日志提示
		session.setDebug(true);

		// 创建邮件 "邮件的发送者" ,"邮件的接受者" , "邮件的抄送者" ,"邮件的密送者"
		MimeMessage message = null;
		try {
			message = createMimeMessage(session, "test20238888@163.com", "195555@qq.com", "19595555513@qq.com",
					"1959282313@qq.com");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 建立连接对象
		Transport transport;
		try {
			transport = session.getTransport();
			// 发送邮件的人的用户名和密码，其中密码为授权码
			try {
				transport.connect("test20238888@163.com", "JZTCLXSZWAKJVZMP");
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			transport.sendMessage(message, message.getAllRecipients());
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	public static MimeMessage createMimeMessage(Session session, String send, String receive, String cReceive,
			String bcReceive) throws Exception {

		MimeMessage mimeMessage = new MimeMessage(session);

		Address address = new InternetAddress(send, "admin", "UTF-8"); //发件人名称
		// mimeMessage.addFrom(数组); 而setFrom(对象)
		mimeMessage.setFrom(address);
		// 标题
		mimeMessage.setSubject("这是标题", "UTF-8");
		// 正文
		mimeMessage.setContent("邮件正文", "text/html;charset=utf-8");
		// 发送日期;当前时间
		mimeMessage.setSentDate(new Date());
		// 收件人类型： TO普通收件人、CC抄送人、BCC密送人
		mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receive, "收件人A", "UTF-8"));
		mimeMessage.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(cReceive, "收件人B", "UTF-8"));
		mimeMessage.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(bcReceive, "收件人B", "UTF-8"));
		// 保存邮件
		mimeMessage.saveChanges();
		return mimeMessage;
	}
}